<?php
/**
 *CustomerRepositories
 * @author tan bing
 * @date 2021-07-19 12:59
 */


namespace Modules\User\Repositories\Eloquent;


use Modules\User\Entities\UserLevel;
use Modules\User\Repositories\Contracts\UserLevelInterface;

class UserLevelRepositories implements UserLevelInterface
{

    /**
     * @param $id
     * @return mixed
     * @author tan bing
     * @date 2021-07-19 13:09
     */
    public function findById($id)
    {
        return UserLevel::find($id);
    }

    /**
     * @param $param
     * @return mixed
     * @author tan bing
     * @date 2021-07-19 13:15
     */
    public function getData($param)
    {
        return UserLevel::addWhere($param)->get();
    }

    public function getUserLevel($param)
    {
        return UserLevel::addWhere($param)->get();
    }

    /**
     * @param $data
     * @return UserLevel|mixed
     * @throws \Exception
     * @author tan bing
     * @date 2021-07-19 13:07
     */
    public function addAndEditDetail($data)
    {
        if(!empty($data['id'])) {
            $userLevelModel = UserLevel::find($data['id']);
            if(!$userLevelModel) {
                $userLevelModel = new UserLevel();
            }
        } else {
            $userLevelModel = new UserLevel();
        }

//        $userLevelModel->status = $data['status'];
        !empty($data['name']) && $userLevelModel->name = $data['name'];
        !empty($data['amount']) && $userLevelModel->amount = $data['amount'];
        !empty($data['discount']) && $userLevelModel->discount = $data['discount'];
        !empty($data['description']) && $userLevelModel->description = $data['description'];
        !empty($data['image']) && $userLevelModel->image = $data['image'];

        if(!$userLevelModel->save()) throw new \Exception('添加或编辑失败，请重试');

        return $userLevelModel;
    }

}